/*
 * 发型素材管理视图
 * @Author: ljb
 * @Date: 2018-05-17 17:07:29
 * @Last Modified by: kanglin
 * @Last Modified time: 2018-06-05 09:06:15
 */
<style lang="less" rel="stylesheet/less" type="text/less" scoped>
    .v-admin-hair-views {
        padding: 20px;
    }
</style>

<template>

	<div class="v-admin-hair-views">

		<Tabs
			type="card"
			@on-click="changeTab">

			<TabPane
				:label="this.$t('common.create')"
				name="新增">
				<!-- 发型素材创建组件 -->
				<hair-create
					ref="create"
					@post="postData"/>

			</TabPane>

			<TabPane
				:label="this.$t('common.check')"
				name="查看">

				<!-- 搜索组件 -->
				<search-form @search="confirmSearch"/>

				<!-- 发型素材列表组件 -->
				<hair-index
					:loading="table_loading"
					:hairs="hair_assets.data"
					@edit="hairDetail"
					@delete="deleteHair"/>

				<br>

				<!-- 分页组件 -->
				<Row
					type="flex"
					justify="center">
					<Page
						:total="hair_assets.total"
						:current="hair_assets.current_page"
						:page-size="query_info.per_page"
						placement="top"
						show-sizer
						show-total
						@on-change="pageChange"
						@on-page-size-change="perPageChange"/>
				</Row>

			</TabPane>
		</Tabs>

		<Modal
			v-model="show_modal"
			:title="$t('hair.update_hair')"
			width="400">

			<!-- 发型素材编辑组件 -->
			<hair-edit
				:hair-detail="hair_detail"
				@update="updateHair"/>

			<div slot="footer"/>

		</Modal>

	</div>

</template>

<script>

import API_LIST from 'src/API_LIST';

import { mapMutations } from 'vuex';

import qs from 'qs';

import hairCreate from './create';
import hairIndex from './index';
import hairEdit from './edit';
import searchForm from './search-form';

export default {

	name: 'HairViews',

	components: {

		hairCreate,

		hairIndex,

		hairEdit,

		searchForm,

	},
	data() {
		return {

			query_info: {

				page: 1,

				per_page: 20,

				keywords: '',

				gender: '',

			}, // 查询信息

			search_info: {

				page: 1,

			}, // 分页信息

			table_loading: false, // 表格更新时的loading

			show_modal: false, // 弹框的控制

			hair_detail: {}, // 发型素材详情

			hair_assets: {}, // 发型素材列表

		};
	},
	methods: {

		...mapMutations('spinShow', [
			'showSpin',
			'closeSpin',
		]),

		/**
         * 切换tab
         * @param  {string} tab_name {tab的name}
         * @return {undefined}
         */
		changeTab(tab_name) {
			if (tab_name === '查看') {
				this.fetchHairList(this.query_info);
			} else {
				this.hair_assets.data = [];
			}
		},

		/**
     * 页码改变
     * @param  {string} pageNumber {当前页码}
     * @return {undefined}
     */
		pageChange(pageNumber) {
			this.search_info.page = pageNumber;

			this.search_info.per_page = this.query_info.per_page;

			this.fetchHairList(this.search_info);
		},

		/**
     * 每页显示数改变
     * @param  {string} per_page {当前每页显示数}
     * @return {undefined}
     */
		perPageChange(per_page) {
			this.search_info.per_page = per_page;

			this.query_info.per_page = per_page;

			this.fetchHairList(this.query_info);
		},

		/**
         * 确认搜索
         * @param  {Object} param {搜索的参数对象}
         * @return {undefined}
         */
		confirmSearch({ keywords, gender }) {
			this.query_info.keywords = keywords;
			this.query_info.gender = gender;

			this.search_info = JSON.parse(JSON.stringify(this.query_info));

			this.fetchHairList(this.query_info);
		},

		/**
         * 新增发型素材的post数据
         * @return {undefined}
         */
		postData(param) {
			this.showSpin(this.$t('common.please_wait'));

			this.$http.post(API_LIST.CUSTOMER_HAIR_ASSETS, param)
				.then(({ data }) => {
					if (Number(data.code) !== 0) throw new Error(data.error);

					this.$Message.success(this.$t('common.create_success'));

					this.$refs.create.handleReset('formValidate');
				})
				.catch((err) => {
					this.$Message.error(err.message);
				})
				.then(this.closeSpin);
		},

		/**
         * 查看发型素材详情
         * @param  {type} hair {当前详情对象}
         * @return {undefined}
         */
		hairDetail(hair) {
			this.hair_detail = hair;

			this.show_modal = true;
		},

		/**
         * 更新发型素材信息
         * @param  {Object} param {需要更新的参数对象}
         * @return {undefined}
         */
		updateHair(id, param) {
			this.showSpin();

			this.$http.post(`${API_LIST.CUSTOMER_HAIR_ASSETS}/${id}`, param)
				.then(({ data }) => {
					if (Number(data.code) !== 0) throw new Error(data.error);

					this.$Message.success(this.$t('common.update_success'));

					this.show_modal = false;

					this.fetchHairList(this.search_info);
				})
				.catch((err) => {
					this.$Message.error(err.message || err.statusText);
				})
				.then(this.closeSpin);
		},

		/**
         * 删除发型素材
         * @param  {string} id {需要删除的发型素材的id}
         * @return {undefined}
         */
		deleteHair(id) {
			this.$Modal.confirm({
				title: this.$t('hair.is_delete'),
				onOk: () => {
					const param = {
						_method: 'DELETE',
					};

					this.table_loading = true;

					this.$http.post(`${API_LIST.CUSTOMER_HAIR_ASSETS}/${id}`, param)
						.then(({ data }) => {
							if (Number(data.code) !== 0) throw new Error(data.error);

							this.fetchHairList(this.search_info);

							this.$Message.success(this.$t('hair.delete_success'));
						})
						.catch((err) => {
							this.$Message.error(err.message || err.statusText);
						})
						.then(() => { this.table_loading = false; });
				},
				onCancel: () => {},
			});
		},

		/**
         * 获取发型素材列表
         * @param  {Object} param {搜索列表的参数}
         * @return {undefined}
         */
		fetchHairList(param) {
			this.table_loading = true;

			const query_str = qs.stringify(param);

			this.$http.get(`${API_LIST.CUSTOMER_HAIR_ASSETS}?${query_str}`)
				.then(({ data }) => {
					if (Number(data.code) !== 0) throw new Error(data.error);

					this.hair_assets = data.hair_assets;
				})
				.catch((err) => {
					this.$Message.error(err.message || err.statusText);
				})
				.then(() => { this.table_loading = false; });
		},
	},
};
</script>
